/* --------------------------------------------------------
 * Copyright (c) Express Scripts, Inc.  All rights reserved.
 * --------------------------------------------------------
 */
package com.aeky.common;

import java.util.Collections;
import java.util.List;

import org.apache.commons.lang.StringUtils;

/**
 * The Class Page.
 * 
 * @param <T> Domain object class
 */
public class Page<T> {

    /** The limit. */
    private int limit = 15;

    /** The offset. */
    private int offset;

    /** The result. */
    private List<T> result = Collections.emptyList();

    /** The total count. */
    private int totalCount = -1;

    /** The sort field. */
    private String sortField;

    /** The group by field. */
    private String groupByField;

    /** The total count limit */
    private int limitTotalCount = -1;

    /**
     * Instantiates a new page.
     */
    public Page() {
    }

    /**
     * Gets the offset.
     * 
     * @return the offset
     */
    public int getOffset() {
        return offset;
    }

    /**
     * Sets the offset.
     * 
     * @param start the new offset
     */
    public void setOffset(int start) {
        this.offset = start;
    }

    /**
     * Gets the limit.
     * 
     * @return the limit
     */
    public int getLimit() {
        return limit;
    }

    /**
     * Sets the limit.
     * 
     * @param pageSize the new limit
     */
    public void setLimit(final int pageSize) {
        this.limit = pageSize;
        if (pageSize < 1) {
            this.limit = 1;
        }
    }

    /**
     * Gets the result.
     * 
     * @return the result
     */
    public List<T> getResult() {
        return result;
    }

    /**
     * Sets the result.
     * 
     * @param result the new result
     */
    public void setResult(final List<T> result) {
        this.result = result;
    }

    /**
     * Gets the total count.
     * 
     * @return the total count
     */
    public int getTotalCount() {
        return totalCount;
    }

    /**
     * Sets the total count.
     * 
     * @param totalCount the new total count
     */
    public void setTotalCount(final int totalCount) {
        this.totalCount = totalCount;
    }

    /**
     * Gets the total pages.
     * 
     * @return the total pages
     */
    public int getTotalPages() {
        if (totalCount < 0) {
            return -1;
        }
        int count = totalCount / limit;
        if (totalCount % limit > 0) {
            count++;
        }
        return count;
    }

    /**
     * Gets the sort field.
     * 
     * @return the sort field
     */
    public String getSortField() {
        return sortField;
    }

    /**
     * Sets the sort field.
     * 
     * @param sortField the new sort field
     */
    public void setSortField(String sortField) {
        this.sortField = sortField;
    }

    /**
     * Gets the group by.
     * 
     * @return the group by
     */
    public String getGroupBy() {
        String groupByString = "";
        if (StringUtils.isNotEmpty(groupByField)) {
            groupByString = " group by " + groupByField;
        }
        return groupByString;
    }

    /**
     * Sets the group by field.
     * 
     * @param groupByField the new group by field
     */
    public void setGroupByField(String groupByField) {
        this.groupByField = groupByField;
    }

    /**
     * Gets the order string.
     * 
     * @return the order string
     */
    public String getOrderString() {
        String orderString = " order by id asc";
        if (StringUtils.isNotEmpty(sortField)) {
            orderString = " order by " + sortField;
        }
        return orderString;
    }

    /**
     * Gets the order string by entity class.
     * 
     * @param className the class name
     * @return the order string by entity class
     */
    public String getOrderStringByEntityClass(String className) {
        String orderString = " order by " + className + "." + "id asc";
        if (StringUtils.isNotEmpty(sortField)) {
            orderString = " order by " + className + "." + sortField;
        }
        return orderString;
    }

    public void setLimitTotalCount(int limitTotalCount) {
        this.limitTotalCount = limitTotalCount;
    }

    public int getLimitTotalCount() {
        return limitTotalCount;
    }
}
